package com.admore.demo.splash;

import static com.admore.sdk.config.AdMoreConstants.AD_APP_ID;
import static com.admore.sdk.config.AdMoreConstants.AD_HEIGHT;
import static com.admore.sdk.config.AdMoreConstants.AD_SPLASH_ADN_ID;
import static com.admore.sdk.config.AdMoreConstants.AD_WIDTH;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.admore.demo.App;
import com.admore.demo.MainActivity;
import com.admore.demo.R;
import com.admore.demo.util.UIUtils;
import com.admore.sdk.ads.AdMoreSplashAd;
import com.admore.sdk.callback.AdMoreLoadListener;
import com.admore.sdk.callback.AdMoreMediaListener;
import com.admore.sdk.sdk.AdMoreSdk;
import com.zcy.base.ui.BaseActivity;

import java.util.HashMap;

public class SplashActivity extends BaseActivity {

    private AdMoreSplashAd splashAd;
    private FrameLayout adMore;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setBackgroundDrawable(null);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        adMore = findViewById(R.id.ad_main);
        ((TextView) findViewById(R.id.tt)).setText("广告splash");
    }

    @Override
    public void initData() {
        startSplashAd();
    }

    private void startSplashAd() {
        if (!AdMoreSdk.init) {
            Log.e(TAG, "AdMoreSdk.init = false");
            gotoMain();
            return;
        }
        HashMap<String, Object> options = new HashMap<>();
        options.put(AD_WIDTH, UIUtils.getScreenWidth(getApplicationContext()));
        options.put(AD_HEIGHT, UIUtils.getScreenHeight(getApplicationContext()));
        options.put(AD_SPLASH_ADN_ID, App.AD_MORE_SPLASH_ADN_ID);
        options.put(AD_APP_ID, App.AD_MORE_APP_ID);
        splashAd = AdMoreSdk.getAdMoreSdkManager().createSplash(this, App.AD_MORE_SPLASH_ID, options);
        splashAd.setLoadListener(new AdMoreLoadListener() {
            @Override
            public void onAdLoadSuccess(String s) {
                Log.d(TAG, "onSplashAdLoad ");
                showSplash();
            }

            @Override
            public void onAdLoadError(Throwable throwable) {
                Log.d(TAG, "onAdLoadError " + throwable);
                Toast.makeText(getApplicationContext(), "广告加载失败！" + throwable.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
        splashAd.loadAd(8000);
    }

    private void showSplash() {
        splashAd.setMediaListener(new AdMoreMediaListener() {
            @Override
            public void onAdClosed() {
                Log.d(TAG, "onAdClosed");
                gotoMain();
            }

            @Override
            public void onAdShow() {
                Log.d(TAG, "onAdShow ecpm:" + splashAd.getAdMoreInfo().getECPM() + " adName:" + splashAd.getAdMoreInfo().getSdkName());
            }
        });
        splashAd.show(true, adMore);
    }

    private void gotoMain() {
        Log.e("splash", "find", new Throwable());
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
    }
}
